import { isBeforeRecordDate } from './salePrice'

export const actionBtns = {
  'edit': {
    name: 'Chage',
    label: '修改',
  },
  'view': {
    name: 'View',
    label: '查看'
  },
  'audit': {
    name: 'Audit',
    label: '审核'
  },
  'auditRecord': {
    name: 'AuditRecord',
    label: '审批记录',
  },
  'delete': {
    name: 'Delete',
    label: '删除'
  },
  'packUp': {
    name: 'PackUp',
    label: '取回'
  }
}
// 没有任何操作按钮的页面列表集合
const noAllCommitTypes = ['updateSkuForSpu'];
// 任何状态都没有修改按钮的页面列表集合
const noAllEditCommitTypes = [
  'saleGroup',
  'saleType',
  'saleSteelType',
  'supplyGoodsStop',
  'acceptance',
  'acceptanceCollect',
  'potentialAcceptance', // 潜在商品验收标准
  'supplyPriceAndRefCity',
  'supplySteelPriceAndRefCity',
  // 'supplySaleArea',
  'addSupplySaleArea',
  'deleteSupplySaleArea',
  'alignmentSupplySaleArea',
  'addedAcceptance',
  'multiSkuUnit',
  'supplyGoodsMainSupply',
  'basicInfo' // 修改基本信息 - 历史
]
// 未提交状态没有修改按钮的页面列表集合
const noEditNOTCommitTypes = [].concat(noAllEditCommitTypes, 'modifySpuCategory');
// 审核失败状态没有修改按钮的页面列表集合
const noEditFAILCommitTypes = [].concat(noAllEditCommitTypes);
// 没有审核按钮的页面列表集合
const noAuditCommitTypes = ['supplyGoods'];
// 没有审批记录按钮的页面列表集合
const noAuditRecordCommitTypes = [
  'basicInfo',
  'supplyGoods',
  // 'acceptance',
  'acceptanceCollect'
];
// 没有取回按钮的页面列表集合
const noPackUpCommitTypes = [
  ...noAllEditCommitTypes,
  ...noAuditRecordCommitTypes,
  'arrivalCycle', // 到货周期
  'multiSkuUnit', // SKU计量单位-多计量
  'editSkuSpec', // 修改SKU规格
  'basicInfoNew' // 基本信息
]
// 含有删除按钮的页面列表集合
const hasDeleteCommitTypes = [
  'supplyPrice', // 进价信息
  'supplySteelPrice', // 进价信息-多计量
  'structureInfo', // 架构信息
  'allManaging', // 管理信息
  // 'supplySaleArea', // 销售区域
  'addSupplySaleArea', // 新增销售区域
  'deleteSupplySaleArea', // 删除销售区域
  'alignmentSupplySaleArea', // 批量修改销售区域
  'supplyGoodsStop', // 停止供货
  'supplyGoodsRecover', // 恢复供货
  'supplyMorePrice', // 多供进价
  'supplyMoreSteelPrice', // 多供进价-多计量
  'supplyGoodsMainSupply', // 修改主供
  'supplyPriceAndRefCity', // 参照城市
  'supplySteelPriceAndRefCity', // 参照城市-多计量
  'salePrice', // 售价信息
  'saleSteelPrice', // 售价信息-多计量
  'saleType', // 售价类型
  'saleSteelType', // 售价类型-多计量
  'basicInfo', // 基本信息
  'addedAcceptance', // 新增验收标准
  'acceptance', // 验收标准
  'supplyGoods', // 供货信息
  'package', // SKU信息
  'arrivalCycle', // 到货周期
];
// 获取未提交的按钮集合
const getNotCommitBtnsFn = (showBtn, { commitType }) => {
  let action = [];
  if (showBtn('查看')) { action.push(actionBtns['view']); }
  if (showBtn('修改') && !noEditNOTCommitTypes.includes(commitType)) {
    action.push(actionBtns['edit']);
  }
  if (hasDeleteCommitTypes.includes(commitType)) {
    action.push(actionBtns['delete']);
  }
  return action;
}
// 获取审核中的按钮集合
const getAuditingBtnsFn = (showBtn, { commitType, workflowRequestId }) => {
  let action = [];
  if (showBtn('查看')) { action.push(actionBtns['view']); }
  if (showBtn('审核') && !noAuditCommitTypes.includes(commitType) && workflowRequestId) {
    action.push(actionBtns['audit']);
  }
  if (!noPackUpCommitTypes.includes(commitType)) {
    action.push(actionBtns['packUp']);
  }
  return action;
}
// 获取审核通过的按钮集合
const getAuditOkBtnsFn = (showBtn, { commitType, workflowRequestId }) => {
  let action = [];
  if (showBtn('查看')) { action.push(actionBtns['view']); }
  if (showBtn('审批记录') && !noAuditRecordCommitTypes.includes(commitType) && workflowRequestId) {
    action.push(actionBtns['auditRecord']);
  }
  return action;
}
// 获取审核失败的按钮集合
const getAuditFailBtnsFn = (showBtn, { commitType, workflowRequestId }) => {
  let action = [];
  if (showBtn('查看')) { action.push(actionBtns['view']); }
  if (showBtn('修改') && !noEditFAILCommitTypes.includes(commitType)) {
    action.push(actionBtns['edit']);
  }
  if (showBtn('审批记录') && !noAuditRecordCommitTypes.includes(commitType) && workflowRequestId) {
    action.push(actionBtns['auditRecord']);
  }
  if (hasDeleteCommitTypes.includes(commitType)) {
    action.push(actionBtns['delete']);
  }
  return action;
}
export const createBtnsForTableAction = (
  { showBtn, data,
    methodList =
    { commitStateNot: getNotCommitBtnsFn,
      commitStateCommit: getAuditingBtnsFn,
      commitOk: getAuditOkBtnsFn,
      commitFail: getAuditFailBtnsFn
    }
  }) => {
  const { commitState, commitType } = data;
  const { commitStateNot, commitStateCommit, commitOk, commitFail } = methodList;
  /*
    commitStateStr  commitState   btn
        未提交          NOT       查看 修改？ 删除？
        审核中         COMMIT     查看 审核？
        审核通过        OK        查看 审批记录？
        审核未通过      FAIL      查看 修改？ 审批记录？ 删除？
  */
  const btnsToMethods = {
    'NOT': commitStateNot,
    'COMMIT': commitStateCommit,
    'OK': commitOk,
    'FAIL': commitFail
  }
  let action = [];
  if (noAllCommitTypes.includes(commitType)) {
    return action;
  }
  if (getSalePriceAction(data)) return getSalePriceAction(data)
  if (data.commitType == 'supplySaleArea') return [actionBtns['view'], actionBtns['auditRecord']] // 历史销售区域特殊处理
  if (btnsToMethods[commitState]) {
    action.push(...btnsToMethods[commitState](showBtn, data));
  }
  return action;
}

// 售价单据处理 - 获取售价单据的表格操作项
export const getSalePriceAction = data => {
  const salePriceKeys = ['salePrice', 'saleSteelPrice']
  const isBefore = isBeforeRecordDate(data?.recordNo) // 早于目标时间即为旧售价单据，只能进行查看操作（包括审批记录）
  const isSalePrice = salePriceKeys.includes(data?.commitType)
  if (isBefore && isSalePrice) return [actionBtns['view'], actionBtns['auditRecord']]
}
